package com.lanxiao.cloud.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

/**
 * @author ：qiaoliang
 * @date ：2021-09-24
 */
@Configuration
public class WebGlobalConfig {

  @Bean
  public CorsWebFilter corsFilter() {
    UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    CorsConfiguration cors = new CorsConfiguration();
    //允许跨域的请求头
    cors.addAllowedHeader("*");
    //允许请求的方法
    cors.addAllowedMethod("*");
    //允许哪些请求源跨域
    cors.addAllowedOrigin("*");
    //是否携带cookie
    cors.setAllowCredentials(false);
    cors.setMaxAge(18000L);
    //允许跨域的路径
    source.registerCorsConfiguration("/**",cors);
    return new CorsWebFilter(source);
  }

}
